{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Even and Odd Bits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: evenOddBit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #奇偶位数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个 <strong>正</strong> 整数 <code>n</code> 。</p>\n",
    "\n",
    "<p>用 <code>even</code> 表示在 <code>n</code> 的二进制形式（下标从 <strong>0</strong> 开始）中值为 <code>1</code> 的偶数下标的个数。</p>\n",
    "\n",
    "<p>用 <code>odd</code> 表示在 <code>n</code> 的二进制形式（下标从 <strong>0</strong> 开始）中值为 <code>1</code> 的奇数下标的个数。</p>\n",
    "\n",
    "<p>返回整数数组<em> </em><code>answer</code><em> </em>，其中<em> </em><code>answer = [even, odd]</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 17\n",
    "<strong>输出：</strong>[2,0]\n",
    "<strong>解释：</strong>17 的二进制形式是 10001 。 \n",
    "下标 0 和 下标 4 对应的值为 1 。 \n",
    "共有 2 个偶数下标，0 个奇数下标。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 2\n",
    "<strong>输出：</strong>[0,1]\n",
    "<strong>解释：</strong>2 的二进制形式是 10 。 \n",
    "下标 1 对应的值为 1 。 \n",
    "共有 0 个偶数下标，1 个奇数下标。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-even-and-odd-bits](https://leetcode.cn/problems/number-of-even-and-odd-bits/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-even-and-odd-bits](https://leetcode.cn/problems/number-of-even-and-odd-bits/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['17', '2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        bin_str = bin(n)[2:]\n",
    "        return [bin_str[-1::-2].count('1'),bin_str[-2::-2].count('1')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        m = 0b1010101010\n",
    "        return [(m >> 1 & n).bit_count(), (n & m).bit_count()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "\n",
    "        ans = [0, 0]\n",
    "\n",
    "        i = 0\n",
    "\n",
    "        while n:\n",
    "\n",
    "            ans[i] += n & 1\n",
    "\n",
    "            n >>= 1\n",
    "\n",
    "            i ^= 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        a = bin(n)[:1:-1]\n",
    "        num1 = 0\n",
    "        num2 = 0\n",
    "        for i in range(0,len(a)):\n",
    "            if i % 2 == 0 and int(a[i]) == 1:\n",
    "                num1 = num1 + 1\n",
    "            if i % 2 == 1 and int(a[i]) == 1:\n",
    "                num2 += 1\n",
    "        return [num1,num2]\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        answer = [0, 0]\n",
    "        flag = True\n",
    "        while n > 0:\n",
    "            if n & 1:\n",
    "                if flag:\n",
    "                    answer[0] += 1\n",
    "                else:\n",
    "                    answer[1] += 1\n",
    "            flag = not flag\n",
    "            n >>= 1\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        sNum = bin(n)[2:][::-1]\n",
    "        even,odd = 0,0\n",
    "        for i in range(len(sNum)):\n",
    "            if sNum[i] == '1':\n",
    "                if i % 2 == 0:\n",
    "                    even += 1\n",
    "                else:\n",
    "                    odd += 1\n",
    "        return [even,odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans =[0,0]\n",
    "        i=0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n>>=1\n",
    "            i^=1\n",
    "        return ans    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        b=bin(n)\n",
    "        b=list(str(b))\n",
    "        b=b[2:]\n",
    "        list.reverse(b)\n",
    "        ans=[0,0]\n",
    "        for i in range(len(b)):\n",
    "            if b[i]=='1':\n",
    "                ans[i%2]=ans[i%2]+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s = '{0:b}'.format(n)\n",
    "        s = s[::-1]\n",
    "        l = len(s)\n",
    "        return [sum([s[i] == '1' for i in range(0, l, 2)]), sum([s[i] == '1' for i in range(1, l, 2)])]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        answer, i = [0, 0], 0\n",
    "        while n > 0:\n",
    "            if n & 1:\n",
    "                answer[i] += 1\n",
    "            i ^= 1\n",
    "            n >>= 1\n",
    "        \n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        return[bin(n)[::-2].count('1'),bin(n)[-2::-2].count('1')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        e = 0\n",
    "        o = 0\n",
    "        for i in range(n):\n",
    "            y = n % 2\n",
    "            n = n // 2\n",
    "            if y == 1:\n",
    "                if i % 2 == 0:\n",
    "                    e += 1\n",
    "                if i % 2 == 1:\n",
    "                    o += 1\n",
    "            if n < 1:\n",
    "                break\n",
    "        return[e, o]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s=bin(n)[2:][::-1]\n",
    "        a=0\n",
    "        b=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='1':\n",
    "                if i%2==0:\n",
    "                    a+=1\n",
    "                else:\n",
    "                    b+=1\n",
    "        return [a,b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        even = 0\n",
    "        odd = 0\n",
    "        for i,b in enumerate(bin(n).replace('0b','')[::-1]):\n",
    "            if b == '1' and i % 2 == 0:\n",
    "                even += 1\n",
    "            if b == '1' and i % 2 != 0:\n",
    "                odd += 1\n",
    "        return [even,odd]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def evenOddBit(self, n: int) -> List[int]:\n",
    "#         sr=bin(n)[2:]\n",
    "#         res=[0]*2\n",
    "#         n=len(sr)\n",
    "#         for i in range(n-1,-1,-2):\n",
    "#             if sr[i]==\"1\":\n",
    "#                 res[0]+=1\n",
    "#             if i-1>=0 and sr[i-1]==\"1\":\n",
    "#                 res[1]+=1\n",
    "#         return res\n",
    "                \n",
    "### 灵茶山：二进制操作\n",
    "class Solution:\n",
    "    def evenOddBit(self,n):\n",
    "        ans=[0,0]\n",
    "        i=0\n",
    "        while n:\n",
    "            ans[i]+=n&1\n",
    "            n>>=1\n",
    "            i=i^1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        # 下标从0 开始，结果输出：[偶数，奇数]\n",
    "        even, odd = 0, 0\n",
    "        binN = bin(n)\n",
    "        for idx, c in enumerate(binN[::-1]):\n",
    "            if c == '1':\n",
    "                if idx % 2 == 0:\n",
    "                    even += 1\n",
    "                else:\n",
    "                    odd += 1\n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        Mask=0x555\n",
    "        return [(Mask&n).bit_count(),(n&(Mask>>1)).bit_count()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        a = bin(n)[2:][::-1]\n",
    "        b = len(a)\n",
    "        even = odd = 0\n",
    "        for i in range(b):\n",
    "            if i%2 == 0 and a[i] == '1':\n",
    "                even += 1\n",
    "            elif i%2 != 0 and a[i] == '1':\n",
    "                odd += 1\n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        odd=0\n",
    "        even=0\n",
    "        num=str(bin(n))[::-1]\n",
    "        for i in range(len(num)):\n",
    "            if i%2==0 and num[i]=='1':\n",
    "                even+=1\n",
    "            if i%2==1 and num[i]=='1':\n",
    "                odd+=1\n",
    "        return [even,odd]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        res=[0]*2\n",
    "        cnt=0\n",
    "        while n:\n",
    "            if n&1:\n",
    "                res[cnt%2] +=1\n",
    "            cnt +=1\n",
    "            n >>=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        even = 0\n",
    "        odd = 0\n",
    "        flag = 1\n",
    "        while n>0:\n",
    "            if n % 2 == 1:\n",
    "                if flag == -1:\n",
    "                    odd += 1\n",
    "                else:\n",
    "                    even += 1\n",
    "            n = n//2\n",
    "            flag = -flag\n",
    "        return [even,odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        x = 0\n",
    "        res = [0, 0]\n",
    "        \n",
    "        while n:\n",
    "            n, r = divmod(n, 2)\n",
    "            if r == 1:\n",
    "                res[x % 2] += 1\n",
    "            x += 1\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        even=odd=0\n",
    "        index=0\n",
    "        while n:\n",
    "            if n%2:\n",
    "                if index%2:\n",
    "                    odd+=1\n",
    "                else:\n",
    "                    even+=1\n",
    "            n>>=1\n",
    "            index+=1\n",
    "        return [even,odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        return[bin(n)[::-1][::2].count('1'),bin(n)[::-1][1::2].count('1')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        res=[0]*2\n",
    "        cnt=0\n",
    "        while n:\n",
    "            n,r=divmod(n,2)\n",
    "            if r:\n",
    "                res[cnt] +=1\n",
    "            cnt ^=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        bin_str = str(bin(n))[2:][::-1]\n",
    "        even = 0\n",
    "        odd = 0\n",
    "        for i, v in enumerate(bin_str):\n",
    "            if i % 2 == 0:\n",
    "                if v == \"1\":\n",
    "                    even += 1\n",
    "\n",
    "            else:\n",
    "                if v == \"1\":\n",
    "                    odd += 1\n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s=bin(n)[2:][::-1]\n",
    "        return s[::2].count('1'),s[1::2].count('1')\n",
    "    \n",
    "\n",
    "     \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        mask = 0x5555\n",
    "        return [(n & mask).bit_count(), (n & (mask >> 1)).bit_count()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s = bin(n)[2:][::-1]\n",
    "        return s[::2].count('1'), s[1::2].count('1')\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        n = str(bin(n))[2:]\n",
    "        n = n[::-1]\n",
    "        res = [0]*2\n",
    "        for i in range(len(n)):\n",
    "            if n[i]=='1':\n",
    "                res[i%2]+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0,0]\n",
    "        tmp = bin(n)[2:]\n",
    "        for i in range(len(tmp)):\n",
    "            if int(tmp[::-1][i])==1:\n",
    "                if i%2:\n",
    "                    ans[1]+=1\n",
    "                else:\n",
    "                    ans[0]+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        a, b = 0, 0\n",
    "        i = 0\n",
    "        while n > 0:\n",
    "            i += 1\n",
    "            if n%2 == 1:\n",
    "                if i%2 == 1:\n",
    "                    a += 1\n",
    "                else:\n",
    "                    b += 1\n",
    "            n //= 2\n",
    "        return a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        mask = 0x5555\n",
    "        even = (n & mask).bit_count()\n",
    "        odd = (n & ~mask).bit_count()\n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        j,o=0,0\n",
    "        newn=bin(n)[2:]\n",
    "        nn=list(reversed(newn))\n",
    "        print(newn)\n",
    "        for i in range(len(nn)):\n",
    "            if i&1 and nn[i]=='1':\n",
    "                j+=1\n",
    "            elif not i&1 and nn[i]=='1':\n",
    "                o+=1\n",
    "        return [o,j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s = bin(n)[2:][::-1]\n",
    "        return s[::2].count(\"1\"), s[1::2].count(\"1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        x = 0\n",
    "        res = [0, 0]\n",
    "        \n",
    "        while n:\n",
    "            n, r = divmod(n, 2)\n",
    "            if r == 1:\n",
    "                res[x] += 1\n",
    "            x ^= 1\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        return[bin(n)[::-2].count('1'),bin(n)[-2::-2].count('1')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        str_n=bin(n)[2:]\n",
    "        reversed_str_n=str_n[::-1]\n",
    "        even=reversed_str_n[::2].count(\"1\")\n",
    "        odd=reversed_str_n[1::2].count(\"1\")\n",
    "        answer=[even,odd]\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "\n",
    "        def bi(n: int) -> List[int]:\n",
    "            if n == 0: return [0]\n",
    "            ans = []\n",
    "            while n != 0:\n",
    "                ans.append(n%2)\n",
    "                n //= 2\n",
    "            return ans\n",
    "\n",
    "        count = bi(n)\n",
    "        even = 0\n",
    "        odd = 0\n",
    "        for i in range(len(count)):\n",
    "            if count[i] and i % 2: odd += 1\n",
    "            elif count[i]: even += 1\n",
    "\n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        lsit=[]\n",
    "        k = bin(n)[2:]\n",
    "        g = k[::-1]\n",
    "        for x in range(len(g)):\n",
    "            if int(g[x]) == 1:\n",
    "                lsit.append(x)\n",
    "        return [sum(1 for m in lsit if m ==0 or m %2==0),sum(1 for m in lsit if m %2==1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s = [0, 0]\n",
    "        for i in range(n.bit_length()):\n",
    "            t = n & 1\n",
    "            if t:\n",
    "                if i % 2:\n",
    "                    s[1] += 1\n",
    "                else:\n",
    "                    s[0] += 1\n",
    "            n >>= 1\n",
    "                \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        kk=[]\n",
    "        while True:\n",
    "            if n==0:\n",
    "                break\n",
    "            else:\n",
    "                #print(\"%\",n%2)\n",
    "                kk.append(n%2)\n",
    "                n=n//2\n",
    "        #print(kk)\n",
    "        ll=len(kk)\n",
    "        uu=0\n",
    "        pp=0\n",
    "        for i in range(0,ll,2):\n",
    "            if kk[i]==1:\n",
    "                uu+=1\n",
    "        for j in range(1,ll,2):\n",
    "            if kk[j]==1:\n",
    "                pp+=1\n",
    "        dd=[]\n",
    "        dd.append(uu)\n",
    "        dd.append(pp)\n",
    "        return dd      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        n = bin(n)[2::][::-1]\n",
    "        a = b = 0\n",
    "        for i in range(0,len(n)):\n",
    "            if n[i] == '1':\n",
    "                if i % 2 == 0:\n",
    "                    a += 1\n",
    "                else:\n",
    "                    b += 1\n",
    "        return [a,b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        bits = 0\n",
    "        res = [0, 0]\n",
    "        while True:\n",
    "            res[bits&1] += n & 1\n",
    "            n >>= 1\n",
    "            bits += 1\n",
    "            if not n: break\n",
    "        \n",
    "        \n",
    "        return res\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        n=bin(n)[2:][::-1]\n",
    "        even=odd=0\n",
    "        for i,x in enumerate(n):\n",
    "            if x=='1':\n",
    "                if i%2==0:\n",
    "                    even+=1\n",
    "                else:\n",
    "                    odd+=1\n",
    "        return [even,odd]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        n = bin(n)[2::][::-1]\n",
    "        a = b = 0\n",
    "        for i in range(0,len(n)):\n",
    "            if n[i] == '1':\n",
    "                if i % 2 == 0:\n",
    "                    a += 1\n",
    "                else:\n",
    "                    b += 1\n",
    "        return [a,b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 1\n",
    "        while n != 0:\n",
    "            ans[i := int(not i)] += n % 2\n",
    "            n >>= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s=bin(n)[2:][::-1]\n",
    "        even=odd=0\n",
    "        for i in range(len(s)):\n",
    "            if i%2==0 and s[i]=='1':\n",
    "                even+=1\n",
    "            elif i%2==1 and s[i]=='1':\n",
    "                odd+=1\n",
    "        return [even,odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        first = second = 0\n",
    "        flag = 1\n",
    "        while n:\n",
    "            if n % 2:\n",
    "                if flag == 1:\n",
    "                    first += 1\n",
    "                else:\n",
    "                    second += 1\n",
    "            flag = - flag\n",
    "            n //= 2\n",
    "        return [first, second]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans,num = [0,0],0\n",
    "        while n:\n",
    "            if n & 1:\n",
    "                ans[num & 1] += 1\n",
    "            num += 1\n",
    "            n >>= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        #0101 0101 0101 0101 0101 0101 0101 0101 0101\n",
    "        MASK = 0x55555555\n",
    "        def cnt(a: int) -> int:\n",
    "            n = 0\n",
    "            while a:\n",
    "                a &= a-1\n",
    "                n += 1\n",
    "            return n\n",
    "        \n",
    "        return [cnt(n&MASK), cnt(n&(MASK<<1))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        num = int(math.log2(n))+1\n",
    "        even, odd = 0, 0\n",
    "        for i in range(num):\n",
    "            if i % 2 == 0 and (n & (1<<i)):\n",
    "                even += 1\n",
    "            elif i % 2 == 1 and (n & (1<<i)):\n",
    "                odd += 1\n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0,0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] +=  n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ret = [0, 0]\n",
    "        for i in range(10):\n",
    "            if n & 1 << i:\n",
    "                ret[i & 1] += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s=bin(n)\n",
    "        print(s)\n",
    "        even=0\n",
    "        ord=0\n",
    "        s=s[::-1]\n",
    "        for i in range(len(s)):\n",
    "            if s[i] =='1':\n",
    "                if i%2==0:\n",
    "                    even+=1\n",
    "                else:\n",
    "                    ord+=1\n",
    "\n",
    "        return [even,ord]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s = [0, 0]\n",
    "        c = bin(n)[2:][::-1]\n",
    "        print(c)\n",
    "        for i in range(len(c)):\n",
    "            if c[i] == '1':\n",
    "                if i % 2 == 0:\n",
    "                    s[0] += 1\n",
    "                else:\n",
    "                    s[1] += 1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        mask = 0x5555\n",
    "        return [(n&mask).bit_count(),(n&(mask>>1)).bit_count()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n:\n",
    "            ans[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        n = bin(n)[2::][::-1]\n",
    "        a = 0\n",
    "        b = 0\n",
    "        for i in range(0,len(n),2):\n",
    "            if n[i] == '1':\n",
    "                a += 1\n",
    "            if i+1 == len(n):\n",
    "                break\n",
    "            if n[i+1] == '1':\n",
    "                b += 1\n",
    "\n",
    "        return [a,b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        n = bin(n)[2::][::-1]\n",
    "        a = b = 0\n",
    "        for i in range(0,len(n)):\n",
    "            if n[i] == '1':\n",
    "                if i % 2 == 0:\n",
    "                    a += 1\n",
    "                else:\n",
    "                    b += 1\n",
    "        return [a,b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> list[int]:\n",
    "        eo, even, odd = True, 0, 0\n",
    "        while n:\n",
    "            x = n & 1\n",
    "            n //= 2\n",
    "            if x and eo:\n",
    "                even += 1\n",
    "            elif x and not eo:\n",
    "                odd += 1\n",
    "            eo = not eo\n",
    "        return [even, odd]\n",
    "    \n",
    "# if __name__ == '__main__':\n",
    "#     print(Solution().evenOddBit(17))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        mask = 0x55555555\n",
    "        even = (n & mask).bit_count()\n",
    "        odd = (n & ~mask).bit_count()\n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        res = [0,0]\n",
    "        i = 0        \n",
    "        while n:\n",
    "            res[i] += n & 1\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ans = [0, 0]\n",
    "        i = 0\n",
    "        while n > 0:\n",
    "            ans[i] += 1 & n\n",
    "            n >>= 1\n",
    "            i ^= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        s = reversed(list(bin(n)[2:]))\n",
    "        odd = 0\n",
    "        even = 0\n",
    "        for i, v in enumerate(s):\n",
    "            if v == '1':\n",
    "                if i & 1:  # odd\n",
    "                    odd += 1\n",
    "                else:\n",
    "                    even += 1\n",
    "        return [even, odd]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        return [(n & 341).bit_count(), (n & 682).bit_count()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        answer = [0,0]\n",
    "        num = bin(n)[2:][::-1]\n",
    "        for i in range(len(num)):\n",
    "            if num[i]=='1' and i%2==0:answer[0] += 1\n",
    "            elif num[i]=='1' and i%2!=0:answer[1] += 1\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        ok = 0\n",
    "        ans = [0] * 2\n",
    "        while n:\n",
    "            if n & 1:\n",
    "                if ok % 2:\n",
    "                    ans[1] += 1\n",
    "                else:\n",
    "                    ans[0] += 1\n",
    "            ok = (ok + 1) % 2\n",
    "            n //= 2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        bin_n_str = str(bin(n))[::-1]\n",
    "        even, odd = 0, 0\n",
    "\n",
    "        for i in range(len(bin_n_str)):\n",
    "            if bin_n_str[i] == '1' and i % 2 == 0:\n",
    "                even += 1\n",
    "            if bin_n_str[i] == '1' and i % 2 == 1:\n",
    "                odd += 1\n",
    "        \n",
    "        return [even, odd]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def evenOddBit(self, n: int) -> List[int]:\n",
    "        string_num = bin(n)[2:] \n",
    "        '''\n",
    "        #10001\n",
    "        #01234\n",
    "        '''\n",
    "        even, odd = 0, 0\n",
    "        for i in range(len(string_num)):\n",
    "            if i % 2 == 0 and string_num[-(i + 1)] == \"1\":\n",
    "                even += 1\n",
    "            elif i % 2 != 0 and string_num[-(i + 1)] == \"1\":\n",
    "                odd += 1\n",
    "        return [even, odd]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
